
package com.tjtt.tdm.base;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

public class PubMethod {
	public static String CheckQueryString(String strValue)
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getParameter(strValue);
	}
	
	public static String GetDecodeString(String url)
	{
		try{
			if (!PubMethod.IsNullOrEmpty(url))
			{
				return java.net.URLDecoder.decode(url,"UTF-8").trim();		
			}
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
		return "";
	}
	
	public static boolean IsNullOrEmpty(String str)
	{
		if (str!=null&&!str.equals(""))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public static void SaveSampleStreamFile(String filePath)
	{
		try{
			HttpServletRequest request = ServletActionContext.getRequest();
			String test = request.getContentType();
			InputStream in = request.getInputStream();
			
			File file = new File(filePath);
			if (file.exists())
			{
				file.delete();
			}
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(filePath);
			
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = in.read(b)) > 0) {
				fos.write(b, 0, i);
			}
			fos.flush();
			fos.close();
			fos = null;
			in = null;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
	}
	
	public static void SaveSampleInputFile(String source,String filePath)
	{
		try{
			FileInputStream in = new FileInputStream(source);
			File file = new File(filePath);
			if (file.exists())
			{
				file.delete();
			}
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(filePath);
			byte[] buffer = new byte[1024]; 
			int len = 0; 
			while ((len = in.read(buffer)) > 0) 
			{ 
				fos.write(buffer, 0, len);
			} 
			fos.flush();
			fos.close();
			in.close();
			fos = null;
			in = null;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
		}
	}
	
	public static String GetTimerName()
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName=formatter.format(cal.getTime());
		return fileName;
	}
	
	public static String GetNowDateString()
	{
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String fileName=formatter.format(cal.getTime());
		return fileName;
	}
	
	public static String GetYYYYMM(){
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM");
		String fileName=formatter.format(cal.getTime());
		return fileName;
	}
	public static String GetWebPath()
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getSession().getServletContext().getRealPath("");
	}
	
	public static PrintWriter GetPrintWriter() 
	{
		try{
		HttpServletResponse response = ServletActionContext.getResponse();
//		response.setContentType("application/XML,charset=GBK");
		response.setContentType("text/html; charset=utf-8"); 
//		response.setHeader("Charset", "GBK");
//		response.setCharacterEncoding("GBK");
//		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		return out;
		}
		catch(Exception exp)
		{
			exp.printStackTrace();
			return null;
		}
	}
	
	public static String GetFlowFilePath()
	{
		return PubMethod.GetWebPath()+"\\flow\\data\\";
	}
	
	
	public static String GetPicPath()
	{
		return PubMethod.GetWebPath()+"\\flow\\PicManage\\";
	}
	
	public static String GetString(Object ob)
	{
		if (ob==null)
		{
			return "";
		}
		else
		{
			return ob.toString();
		}
	}
	
	public static String GetFlowid(String name)
	{
		Properties  p = GenericsUtils.loadProperty("flow.properties");
		return p.getProperty(name);
	}
	
	public static String GetFilepathfromproperties(String name)
	{
		Properties  p = GenericsUtils.loadProperty("file.properties");
		return p.getProperty(name);
	}
	
	public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } finally {
            // 关闭流
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }
	
	public static void exportFile(String filepath,HttpServletResponse response){
		File file=new File(filepath);//构造要下载的文件	
		if(file.exists()){
			InputStream ins;
			try {
				ins = new FileInputStream(filepath);
				//构造一个读取文件的IO流对象
				BufferedInputStream bins=new BufferedInputStream(ins);//放到缓冲流里面
				OutputStream outs=response.getOutputStream();//获取文件输出IO流
				BufferedOutputStream bouts=new BufferedOutputStream(outs);
				response.setContentType("application/x-download");//设置response内容的类型
				response.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(file.getName(), "UTF-8"));//设置头部信息
				int bytesRead = 0;
				byte[] buffer = new byte[8192];
				//开始向网络传输文件流
				while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
					bouts.write(buffer, 0, bytesRead);
				}
				bouts.flush();//这里一定要调用flush()方法
				ins.close();
				bins.close();
				outs.close();
				bouts.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
			}
		}else{
			System.out.println("下载的文件不存在");
		}
	}
	
	/**
	 * 返回年月日
	 * @param strdate
	 * @return
	 */
	public static String getFormatString(Object strdate){
		if (strdate!=null){
			String[] arrayDate = strdate.toString().split("-");
			if (arrayDate.length>2){
				return arrayDate[0]+"年"+arrayDate[1]+"月"+arrayDate[2]+"日";
			}
			else{
				return "";
			}
		}
		else{
			return "";
		}
		
	}
}
